<template>
	<view>
		<view class="goods-addCart" v-if="type == 1">
			<view class="minus" v-if="goods.goodsNum>0" @click="minus">
				<u-icon name="minus" color="#00CBCB" size="28"></u-icon>
			</view>
			<view class="num" v-if="goods.goodsNum>0">
				{{goods.goodsNum}}
			</view>
			<view class="plus" @click="plus">
				<u-icon name="plus" color="#fff" size="28"></u-icon>
			</view>
		</view>
		<view v-if="type == 2" class="type2"  @click="plus">
			加入购物车
		</view>
	</view>
</template>

<script>
	import { mapActions, mapState, mapMutations } from 'vuex';
	import bezier from '@/common/utils/bezier.js'
	export default{
		props:{
			type:{
				type:[String, Number],
				default:1
			},
			goods:{
				type:Object
			}
		},
		data(){
			return {
				disabled:false,
				cartNum:0,
				cartItemElc:{
				    left:0,
				    top:0
				},
			}
		},
		created() {
			// this.getCartCount()
		},
		methods:{
			...mapActions(['ActSaveCart','ActGetCartCount','ActGetCartList','ActUpdateCartCheck','ActGetCartCoupon']),
			...mapMutations(['CHECKCART','UPDATECARTNUM','GOODS_UPDATECARTNUM','STOREGOODS_UPDATECARTNUM','GOODS_COLLOCATION_UPDATECARTNUM','GOODS_DETAIL_UPDATECARTNUM']),
			async getCartCount(){
				const res = await this.ActGetCartCount({
					storeId:uni.getStorageSync('storeId'),
					goodsId:this.goods.goodsId
				})
				if(res.success){
					this.cartNum = res.data
				}
			},
			async saveCart(num,first){
				this.disabled=true
				const res = await this.ActSaveCart({
					storeId:this.goods.storeId,
					goodsNum:num,
					activityId:this.goods.activityId,
					activityType:this.goods.activityType,
					goodsId:this.goods.goodsId,
					goodsPrice:this.goods.goodsPrice,
					saveType:'0'
				})
				this.disabled=false
				if(res.success){
					await this.ActGetCartCoupon({
						storeId:uni.getStorageSync('storeId')
					})
					this.GOODS_DETAIL_UPDATECARTNUM(res.data.cartGoodsNum)
					this.GOODS_UPDATECARTNUM([{num:res.data.cartGoodsNum,goodsId:this.goods.goodsId}])
					this.STOREGOODS_UPDATECARTNUM([{num:res.data.cartGoodsNum,goodsId:this.goods.goodsId}])
					this.GOODS_COLLOCATION_UPDATECARTNUM([{num:res.data.cartGoodsNum,goodsId:this.goods.goodsId}])
					if(first){
						this.ActGetCartList({
							storeId:uni.getStorageSync('storeId')
						})
					}else{
						this.UPDATECARTNUM([{num:res.data.cartGoodsNum,goodsId:this.goods.goodsId}])
						this.ActUpdateCartCheck({cartIds:res.data.cartId,isCheck:1})
						this.CHECKCART({
							name:res.data.cartId,
							value:true
						})
					}
				}
			},
			minus(){
				if(!this.disabled){
					if(this.goods.goodsNum<=Number(this.goods.minBuyQty)){
						this.saveCart(0,this.goods.goodsNum==1)
					}else{
						this.saveCart(this.goods.goodsNum-1,this.goods.goodsNum==1)
					}
				}
			},
			plus(){
				const num = this.goods.goodsNum-0+1
				if(!this.disabled){
					if(num<=Number(this.goods.minBuyQty)){
						this.saveCart(Number(this.goods.minBuyQty),this.goods.goodsNum==0)
					}else if(this.goods.goodsNum>=Number(this.goods.maxBuyQty)){
						uni.showModal({
							title: '提示',
							content: '已是最大购买数量！',
							showCancel:false
						});
					}else{
						this.saveCart(num,this.goods.goodsNum==0)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.type2{
		padding: 8px 12px;
		background-color: $u-type-primary;
		color: #fff;
		border-radius: 62px;
		font-size: 14px;
		display: inline-block;
	}
	.goods-addCart{
		display: flex;
		align-items: center;
		
		.ball {
			height: 40rpx;
			width: 40rpx;
			background: $u-type-primary;
			border-radius: 50%;
			position: fixed;
			left:0px;
			z-index:100;
		}
		.minus, .plus{
			width: 48rpx;
			height: 48rpx;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.minus{
			border: 1rpx solid $u-type-primary;
		}
		.plus{
			background-color: $u-type-primary;
			border: 1rpx solid $u-type-primary;
		}
		.num{
			width: 52rpx;
			text-align: center;
			font-size: 24rpx;
		}
	}
</style>
